package android.slc.icr.core.service.impl;

import android.slc.icr.core.entity.IcrUserInfo;
import android.slc.icr.core.service.IcrChartRoomMemberService;
import android.slc.icr.core.service.IcrUserChartRoomService;
import cn.hutool.core.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 简单的房间成员服务 一个房间搭配一个
 * 这里面的信息没有进行持久化
 *
 * @author slc
 * @date 2022/05/10 上午 11:52
 */
public class SimpleIcrChartRoomMemberService implements IcrChartRoomMemberService {

    public IcrUserChartRoomService icrUserChartRoomService;

    /**
     * 用户集合，使用map的形式呈现，key为成员的id，value为成员的用户信息和状态，通过map获取方便
     */
    public final Map<String, Map<String, IcrUserInfo>> roomIcrUserInfoMap = new HashMap<>();

    public SimpleIcrChartRoomMemberService(IcrUserChartRoomService icrUserChartRoomService) {
        this.icrUserChartRoomService = icrUserChartRoomService;
    }

    private Map<String, IcrUserInfo> getUserInfoMapByRoomId(String roomId) {
        Map<String, IcrUserInfo> userInfoMap = roomIcrUserInfoMap.computeIfAbsent(roomId, new Function<String, Map<String, IcrUserInfo>>() {
            @Override
            public Map<String, IcrUserInfo> apply(String s) {
                return new HashMap<>();
            }
        });
        return userInfoMap;
    }

    @Override
    public boolean addMember(IcrUserInfo icrUserInfo) {
        if (icrUserInfo == null) {
            return false;
        }
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(icrUserInfo.getRelateId());
        userInfoMap.put(icrUserInfo.getUserId(), icrUserInfo);
        icrUserChartRoomService.attachUserByRoom(icrUserInfo);
        return true;
    }

    @Override
    public boolean updateNumber(IcrUserInfo icrUserInfo) {
        if (icrUserInfo == null) {
            return false;
        }
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(icrUserInfo.getRelateId());
        userInfoMap.put(icrUserInfo.getUserId(), icrUserInfo);
        return true;
    }

    @Override
    public IcrUserInfo removeMember(IcrUserInfo icrUserInfo) {
        if (icrUserInfo == null) {
            return null;
        }
        icrUserChartRoomService.detachUserByRoom(icrUserInfo);
        return removeMember(icrUserInfo.getRelateId(), icrUserInfo.getUserId());
    }

    @Override
    public IcrUserInfo removeMember(String roomId, String userId) {
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(roomId);
        return userInfoMap.remove(userId);
    }

    @Override
    public boolean containsMember(IcrUserInfo icrUserInfo) {
        if (icrUserInfo == null) {
            return false;
        }
        return containsMember(icrUserInfo.getRelateId(), icrUserInfo.getUserId());
    }

    @Override
    public boolean containsMember(String roomId, String userId) {
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(roomId);
        return userInfoMap.containsKey(userId);
    }

    @Override
    public IcrUserInfo findUserInfo(String roomId, String userId) {
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(roomId);
        return userInfoMap.get(userId);
    }

    @Override
    public List<IcrUserInfo> getAllMember(String roomId) {
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(roomId);
        List<IcrUserInfo> icrUserInfoList = new ArrayList<>(userInfoMap.values());
        CollectionUtil.sort(icrUserInfoList, (o1, o2) -> {
            if (o1.isCreate() == o2.isActor()) {
                return 0;
            }
            return o1.isCreate() ? 1 : -1;
        });
        return icrUserInfoList;
    }

    @Override
    public long getMemberSize(String roomId) {
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(roomId);
        return userInfoMap.size();
    }

    @Override
    public void removeAll(String roomId) {
        Map<String, IcrUserInfo> userInfoMap = getUserInfoMapByRoomId(roomId);
        userInfoMap.keySet().forEach(userId -> icrUserChartRoomService.detachUserByRoom(userInfoMap.get(userId)));
        userInfoMap.clear();
    }
}
